// user_pkg/pages/login/login.js
const app = getApp()
Page({
  data: {
    imageCaptcha: '',
    captchaKey: '',
    mobileStatus: 'warning',
    mobileTips: '请输入手机号码',
    mobile: '',
    captchaCodeStatus: 'warning',
    captchaCodeTips: '请输入图形验证码',
    captchaCode: '',
    smsCodeStatus: 'warning',
    smsCodeTips: '请输入短信验证码',
    smsCode: '',
    time: -1
  },
  onLoad() {
    this.getImageCaptcha()
  },
  // 获取图形验证码
  async getImageCaptcha() {
    const res = await wx.http.get('/captcha/image')
    const imageCaptcha = res.data.data.base64
    const captchaKey = res.data.data.key
    this.setData({imageCaptcha, captchaKey})
  },
  // 校验手机号
  mobileChange(ev) {
    const mobile = ev.detail.value
    const mobileRule = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
    if(mobile === '') {
      this.setData(
        {
          mobileStatus: 'warning',
          mobileTips: '请输入手机号码',
          mobile: ''
        }
      )
    } else if(!mobileRule.test(mobile)) {
      this.setData(
        {
          mobileStatus: 'warning',
          mobileTips: '请输入正确的手机号码'
        }
      )
    } else {
      this.setData(
        {
          mobileStatus: '',
          mobileTips: '',
          mobile
        }
      )
    }
  },
  // 校验图形验证码
  captchaCodeChange(ev) {
    const captchaCode = ev.detail.value
    if(captchaCode === '') {
      this.setData(
        {
          captchaCode: '',
          captchaCodeStatus: 'warning',
          captchaCodeTips: '请输入图形验证码'
        }
      )
    } else {
      this.setData(
        {
          captchaCode,
          captchaCodeStatus: '',
          captchaCodeTips: ''
        }
      )
    }
  },
  // 校验短信验证码
  smsCodeChange(ev) {
    const smsCode = ev.detail.value
    if(smsCode === '') {
      this.setData(
        {
          smsCode: '',
          smsCodeStatus: 'warning',
          smsCodeTips: '请输入短信验证码'
        }
      )
    } else {
      this.setData(
        {
          smsCode,
          smsCodeStatus: '',
          smsCodeTips: '开发环境验证码为246810'
        }
      )
    }
  },
  // 获取短信验证码
  async getSmsCode() {
    const form = {
      captchaCode: this.data.captchaCode,
      captchaKey: this.data.captchaKey,
      mobile: this.data.mobile
    }
    if(this.data.mobileStatus !== 'warning' && this.data.captchaCodeStatus !== 'warning') {
      const res = await wx.http.post('/captcha/sendSmsCaptcha', {form})
      if(res.data.message !== '小智提醒：测试环境短信验证码为：246810') {
        this.setData({
          captchaCodeStatus: 'warning',
          captchaCodeTips: res.data.message
        })
      } else {
        this.setData({
          time: 60
        })
        this.countdown()
      }
    }
  },
  // 短信验证码倒计时
  countdown() {
    let time = this.data.time
    if(time > 0) {
      time--
      this.setData({time})
      setTimeout(this.countdown, 1000)
    } else {
      this.setData({time: -1})
    }
  },
  // 登录
  async login() {
    const form = {
      isParty: false,
      mobile: this.data.mobile,
      partyData: {},
      smsCode: this.data.smsCode
    }
    if(this.data.mobileStatus !== 'warning' && this.data.captchaCodeStatus !== 'warning' && this.data.smsCodeStatus !== 'warning') {
      const res = await wx.http.post('/passport/login', {form})
      if(res.data.message !== '小智提示: 测试环境短信验证码为【246810】') {
        wx.showToast({
          title: '登录成功，正在跳转',
          icon: 'success',
          duration: 2000
        })
        app.setToken('token', res.data.data.token)
        wx.http.token = await wx.getStorageSync('token')
        await this.getUserInfo()
        wx.navigateBack({
          delta: 1
        })
      } else {
        this.setData({
          smsCodeStatus: '',
          smsCodeTips: '开发环境验证码为246810'
        })
      }
    }
  },
  // 获取用户信息添加登录态
  async getUserInfo() {
    const res = await wx.http.get('/user/info')
    if(res.data.status !== 401) {
      const userId = res.data.data.userInfo.user_id
      if(userId) {
        getApp().setUserLoginStatus(userId)
      }
    } else {
      getApp().setUserLoginStatus('')
    }
  }
})